<?php
// 索引队列
set_time_limit(0);
ini_set('display_errors', 'on');
error_reporting(E_ERROR | E_WARNING | E_PARSE);

$data_path = '/home/www/qisuu_data/data/';
include_once('/usr/local/xunsearch/sdk/php/lib/XS.php');

$xs = new XS('books');
$index  = $xs->index;
//$index->clean();

$times[] = microtime(true);
$files = scandir($data_path);
$times[] = microtime(true);
$total = count($files);
foreach ($files as $k=>$file) {
    if(!stristr($file, '.html')) {
        continue;
    }

    $tmp = explode('%2F', $file);
    $tmp = end($tmp);
    $tmp = str_ireplace('Shtml','',$tmp);
    $tmp = str_ireplace('.html','',$tmp);
    $tmp = (int) $tmp;
    if($tmp <= 36906 ) {
        var_dump("{$k}/{$total}, id:{$tmp}小于36906, {$file}");
        continue;
    }

    $contents = file_get_contents($data_path.$file);
    $contents = json_decode($contents, true);

    if(!$contents) {
        var_dump("{$k}/{$total}.NULL");
        continue;
    }

    $md5 = md5($file);
    $addtime = strtotime($contents['info'][4]);
    $addtime = $addtime ? $addtime : strtotime($contents['info'][3]);
    $addtime = $addtime ? $addtime : time();

    $author = $contents['info'][6];
    $author = $author ? $author : $contents['info'][5];
    $index_data = array(
        'md5id' => $md5,
        'index' => $md5,
        'title' => $contents['title'],
        'description' => $contents['description'],
        'classname' => $contents['classname'],
        'typename' => $contents['typename'],
        'author' => $author,
        'addtime' => $addtime, //书籍发布时间
        'info' => implode('||', $contents['info']),
        'thumb' => $contents['thumb'],
        'download' => is_array($contents['download']) ? implode('||', $contents['download']) : $contents['download'],
        'indextime' => time(), //索引添加时间
    );

    // 创建文档对象
    $doc = new XSDocument;
    $doc->setFields($index_data);

    // 更新到索引数据库中
    $result = $index->update($doc);

    var_dump("{$k}/{$total}.".$contents['title']);
}

$times[] = microtime(true);
var_dump($times, date('Y-m-d H:i:s'));
